home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cocktail / reuse.lha / reuse / m2c / System.c < prev    next >
C/C++ Source or Header  |  1992-08-18  |  6KB  |  253 lines

  1. /* $Id: System.c,v 1.6 1992/03/24 13:09:15 grosch rel $ */
  2.  
  3. /* $Log:
  4.  */
  5.  
  6. /* Ich, Doktor Josef Grosch, Informatiker, Jan. 1992 */
  7.  
  8. /* interface for machine dependencies */
  9.  
  10. /* compilation with the option -DUNIX uses UNIX system calls for IO (efficient),
  11.    otherwise the C library routines are used for IO (portable).            */
  12.  
  13. # ifdef __STDC__
  14. # define ARGS(parameters)    parameters
  15. # else
  16. # define ARGS(parameters)    ()
  17. # endif
  18.  
  19. # ifndef bool
  20. # define bool char
  21. # endif
  22. # define tFile int
  23.  
  24. # ifdef m68000
  25. # define hz 50
  26. # else
  27. # define hz 60
  28. # endif
  29.  
  30. /* binary IO */
  31.  
  32. extern tFile    OpenInput    ARGS((char * FileName));
  33.             /* Opens the file whose name is given by the    */
  34.             /* string parameter 'FileName' for input.    */
  35.             /* Returns an integer file descriptor.        */
  36.  
  37. extern tFile    OpenOutput    ARGS((char * FileName));
  38.             /* Opens the file whose name is given by the    */
  39.             /* string parameter 'FileName' for output.    */
  40.             /* Returns an integer file descriptor.        */
  41.  
  42. extern int    Read        ARGS((tFile File, char * Buffer, int Size));
  43.             /* Reads 'Size' bytes from file 'tFile' and    */
  44.             /* stores them in a buffer starting at address    */
  45.             /* 'Buffer'.                    */
  46.             /* Returns the number of bytes actually read.    */
  47.  
  48. extern int    Write        ARGS((tFile File, char * Buffer, int Size));
  49.             /* Writes 'Size' bytes from a buffer starting    */
  50.             /* at address 'Buffer' to file 'tFile'.        */
  51.             /* Returns the number of bytes actually written.*/
  52.  
  53. extern void    Close        ARGS((tFile File));
  54.             /* Closes file 'tFile'.                */
  55.  
  56. extern bool IsCharacterSpecial    ARGS((tFile File));
  57.             /* Returns TRUE when file 'tFile' is connected    */
  58.             /* to a character device like a terminal.    */
  59.  
  60.  
  61. /* calls other than IO */
  62.  
  63. extern char *    SysAlloc    ARGS((long ByteCount));
  64.             /* Returns a pointer to dynamically allocated    */
  65.             /* memory space of size 'ByteCount' bytes.    */
  66.             /* Returns NIL if space is exhausted.        */
  67.  
  68. extern long    Time        ();
  69.             /* Returns consumed cpu-time in milliseconds.    */
  70.  
  71. extern int    GetArgCount    ();
  72.             /* Returns number of arguments.            */
  73.  
  74. extern void    GetArgument    ARGS((int ArgNum, char * Argument));
  75.             /* Stores a string-valued argument whose index    */
  76.             /* is 'ArgNum' in the memory area 'Argument'.    */
  77.  
  78. extern void    PutArgs        ARGS((int Argc, char * * Argv));
  79.             /* Dummy procedure that passes the values    */
  80.             /* 'argc' and 'argv' from Modula-2 to C.    */
  81.  
  82. extern int    ErrNum        ();
  83.             /* Returns the current system error code.    */
  84.  
  85. extern int    System        ARGS((char * String));
  86.             /* Executes an operating system command given    */
  87.             /* as the string 'String'. Returns an exit or    */
  88.             /* return code.                    */
  89.  
  90. extern void    Exit        ARGS((int Status));
  91.             /* Terminates program execution and passes the    */
  92.             /* value 'Status' to the operating system.    */
  93.  
  94. extern void    BEGIN_System    ();
  95.             /* Dummy procedure with empty body.        */
  96.  
  97.  
  98. # ifndef UNIX
  99.  
  100. # include <stdio.h>
  101. # define NOFILES 32
  102.  
  103. static char IsLineBuffered [NOFILES] = { 1, 1, 1, };
  104.  
  105. static FILE *    FileStore [NOFILES] = {
  106.    stdin, stdout, stderr, NULL, NULL, NULL, NULL, NULL,
  107.    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  108.    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  109.    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  110. };
  111.  
  112. static tFile FileToInt (File)
  113.    FILE *    File;
  114. {
  115.    register int    f = fileno (File);
  116.    FileStore [f] = File;
  117.    return f;
  118. }
  119.  
  120. static FILE * IntToFile (File)
  121.    tFile    File;
  122. {
  123.    return FileStore [File];
  124. }
  125.  
  126. # endif
  127.  
  128. /* binary IO */
  129.  
  130. # include <fcntl.h>
  131. # include <sys/types.h>
  132. # include <sys/stat.h>
  133.  
  134. bool IsCharacterSpecial (File) tFile File;
  135. {
  136.    struct stat    buf;
  137.    (void) fstat (File, & buf);
  138.    return (0020000 & buf.st_mode) == 0020000;
  139. }
  140.  
  141. tFile OpenInput (FileName)
  142.    char *    FileName;
  143. {
  144. # ifndef UNIX
  145.    tFile File = FileToInt (fopen (FileName, "r"));
  146.    IsLineBuffered [File] = IsCharacterSpecial (File);
  147.    return File;
  148. # else
  149.    return open (FileName, O_RDONLY);
  150. # endif
  151. }
  152.  
  153. tFile OpenOutput (FileName)
  154.    char *    FileName;
  155. {
  156. # ifndef UNIX
  157.    return FileToInt (fopen (FileName, "w"));
  158. # else
  159.    return creat (FileName, 0666);
  160. # endif
  161. }
  162.  
  163. int Read (File, Buffer, Size)
  164.    tFile    File;
  165.    char *    Buffer;
  166.    int        Size;
  167. {
  168. # ifndef UNIX
  169.    if (IsLineBuffered [File]) {
  170.       Buffer [0] = '\0';
  171.       (void) fgets (Buffer, Size, IntToFile (File));
  172.       return strlen (Buffer);
  173.    } else
  174.       return fread (Buffer, 1, Size, IntToFile (File));
  175. # else
  176.    return read (File, Buffer, Size);
  177. # endif
  178. }
  179.  
  180. int Write (File, Buffer, Size)
  181.    tFile    File;
  182.    char *    Buffer;
  183.    int        Size;
  184. {
  185. # ifndef UNIX
  186.    return fwrite (Buffer, 1, Size, IntToFile (File));
  187. # else
  188.    return write (File, Buffer, Size);
  189. # endif
  190. }
  191.  
  192. void Close (File)
  193.    tFile    File;
  194. {
  195. # ifndef UNIX
  196.    (void) fclose (IntToFile (File));
  197. # else
  198.    (void) close (File);
  199. # endif
  200. }
  201.  
  202. /* calls other than IO */
  203.  
  204. /* # include <malloc.h> */
  205.  
  206. char * SysAlloc (ByteCount) long ByteCount; { return (char *) malloc ((unsigned) ByteCount); }
  207.  
  208. # include <sys/times.h>
  209.  
  210. long Time ()
  211. {
  212.    struct tms    buffer;
  213.    (void) times (& buffer);
  214.    return (buffer.tms_utime + buffer.tms_stime) * 1000 / hz;
  215. }
  216.  
  217. static int    argc;
  218. static char * *    argv;
  219.  
  220. int GetArgCount ()
  221. {
  222.    return argc;
  223. }
  224.  
  225. void GetArgument (ArgNum, Argument)
  226.    int        ArgNum;
  227.    char *    Argument;
  228. {
  229.    register int    i = 0;
  230.    for (;; i ++)
  231.       if ((Argument [i] = argv [ArgNum][i]) == '\0') return;
  232. }
  233.  
  234. void PutArgs (Argc, Argv)
  235.    int        Argc;
  236.    char * *    Argv;
  237. {
  238.    argc = Argc;
  239.    argv = Argv;
  240. }
  241.  
  242. # include <errno.h>
  243.  
  244. int ErrNum () { return errno; }
  245.  
  246. int System (String) char * String; { return system (String); }
  247.  
  248. extern void exit ();
  249.  
  250. void Exit (Status) int Status; { exit (Status); }
  251.  
  252. void BEGIN_System () {}
  253.